package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.service.FileService;
import com.wuseguang.springcloud.util.CallbackSample;
import com.wuseguang.springcloud.util.CodeUtil;
import com.wuseguang.springcloud.util.Result;
import com.wuseguang.springcloud.util.UploadSample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 文件服务实现类
 */
@Service
public class FileServiceImpl implements FileService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());


    @Override
    public Result upload(String schoolId, MultipartFile file, int type) {

        logger.info("上传文件/图片/人脸");

        //文件url
        String url = CallbackSample.saveFile(file,schoolId,type);
        logger.info("url:"+url);
        //判断文件是否上传失败
        if ("error".equals(url)){
            return new Result(CodeUtil.UPLOAD_FAILURE);
        }
        return new Result((Object)url);
    }

    @Override
    public Result batchUpload(String schoolId, MultipartFile[] file, int type) {
        //图片路径
        List<String> list = new ArrayList<>();
        //创建
        StringBuilder errorString = new StringBuilder("第");

        //图片rul
        String url =null;
        logger.info("批量上传文件/图片/人脸");
        for (int i = 0;i<file.length;i++){
            //文件url
            url = CallbackSample.saveFile(file[i],schoolId,type);
            //判断文件是否上传失败
            if ("error".equals(url)){
                errorString.append(","+(i+1));
                continue;
            }
            list.add(url);
        }

        //如果错信息大于2
        if(errorString.toString().length()>2){
            return new Result(CodeUtil.UPLOAD_FAILURE);
        }

        return new Result(list);
    }
}